package com.tvunetworks.center.user.controller;

import com.github.pagehelper.PageInfo;
import com.tvunetworks.center.common.model.LoginUser;
import com.tvunetworks.center.common.util.Result;
import com.tvunetworks.center.common.util.ResultUtil;
import com.tvunetworks.center.user.annotation.Logined;
import com.tvunetworks.center.user.model.param.AddGroupParam;
import com.tvunetworks.center.user.model.param.PageCustomizedUserGroupParam;
import com.tvunetworks.center.user.model.vo.CustomizedUserGroupVo;
import com.tvunetworks.center.user.service.CustomizedUserGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @program: tvucc-aggregator
 * @description: CustomizedUserGroupController
 * @author lebronchen
 * @create: 2020-03-13 09:10
 **/
@RestController
@RequestMapping("/userGroup")
@Slf4j
public class CustomizedUserGroupController {

    @Autowired
    private CustomizedUserGroupService customizedUserGroupService;

    /**
     *
     * @param loginUser loginUser
     * @param param param
     * @return Result
     */
    @PostMapping("/addGroup")
    public Result addGroup(@Logined LoginUser loginUser, @RequestBody AddGroupParam param) {
        if (StringUtils.isEmpty(param.getGroup())) {
            return ResultUtil.paramError();
        }
        param.setUserId(loginUser.getId());
        customizedUserGroupService.addGroup(param);
        return ResultUtil.success();
    }

    /**
     *
     * @param loginUser loginUser
     * @param param param
     * @return Result
     */
    @PostMapping("/deleteGroup")
    public Result deleteGroup(@Logined LoginUser loginUser, @RequestBody AddGroupParam param) {
        if (StringUtils.isEmpty(param.getId())) {
            return ResultUtil.paramError();
        }
        customizedUserGroupService.deleteGroup(loginUser.getId(), param.getId());
        return ResultUtil.success();
    }

    /**
     *
     * @param loginUser loginUser
     * @param param param
     * @return Result
     */
    @PostMapping("/updateGroup")
    public Result updateGroup(@Logined LoginUser loginUser, @RequestBody AddGroupParam param) {
        if (StringUtils.isEmpty(param.getId())) {
            return ResultUtil.paramError();
        }
        param.setUserId(loginUser.getId());
        customizedUserGroupService.updateGroup(loginUser.getId(), param);
        return ResultUtil.success();
    }

    /**
     *
     * @param loginUser loginUser
     * @param param param
     * @return Result
     */
    @PostMapping("/listUserGroup")
    public Result listUserGroup(@Logined LoginUser loginUser, @RequestBody PageCustomizedUserGroupParam param) {
        param.setUserId(loginUser.getId());
        PageInfo<CustomizedUserGroupVo> list = customizedUserGroupService.listUserGroup(param);
        return ResultUtil.success(list);
    }

}
